package com.example.service;

import io.etcd.jetcd.Client;
import io.etcd.jetcd.KeyValue;
import io.etcd.jetcd.Txn;
import io.etcd.jetcd.Watch;
import io.etcd.jetcd.cluster.MemberListResponse;
import io.etcd.jetcd.kv.DeleteResponse;
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.kv.PutResponse;
import io.etcd.jetcd.lease.LeaseKeepAliveResponse;
import io.etcd.jetcd.lease.LeaseRevokeResponse;
import io.etcd.jetcd.lease.LeaseTimeToLiveResponse;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface EtcdService {


    /***
     * 获取Etcd客户端
     * @return
     */
    public Client getClient();

    /***
     * 判断key是否存在
     * @param key
     * @return
     * @throws Exception
     */
    public boolean isExist(String key) throws Exception;

    /***
     * 设置一个key value
     * @param key
     * @param value
     * @return
     */
    public CompletableFuture<PutResponse> putAsync(String key, byte[] value);

    /***
     * `put`一个key value，并设置ttl
     * @param key
     * @param value
     * @param ttlSeconds
     * @return
     * @throws Exception
     */
    public long putByTls(String key, byte[] value, long ttlSeconds) throws Exception;


    /***
     * 设置一个key
     * @param key
     * @param value
     * @throws Exception
     */
    public void putSync(String key, byte[] value) throws Exception;


    /****
     * 设置kv
     * @param key
     * @param value
     * @return
     * @throws Exception
     */
    public void put(String key, byte[] value) throws Exception;


    /****
     *
     * @param key
     * @param value
     * @throws Exception
     */
    public void putAutoLease(String key, byte[] value, long ttlSeconds) throws Exception;


    /***
     * 删除一个key
     * @param key
     * @return
     */
    public CompletableFuture<DeleteResponse> deleteAsync(String key);


    /***
     * 删除一个key
     * @param key
     * @throws Exception
     */
    public void deleteSync(String key) throws Exception;


    /***
     *  获取一个key对应的value
     * @param key
     * @return
     * @throws Exception
     */
    public CompletableFuture<GetResponse> getAsync(String key) throws Exception;


    /****
     * 同步获取kv信息
     * @param key
     * @throws Exception
     */
    public List<KeyValue> getSync(String key) throws Exception;


    /****
     * 获取kv的前缀
     * @param key
     * @return
     * @throws Exception
     */
    public List<KeyValue> getChildList(String key) throws Exception;

    /***
     * 获取一个key的lease信息
     * @param grantId
     * @return
     * @throws Exception
     */
    public LeaseTimeToLiveResponse getKeyLease(long grantId) throws Exception;

    /***
     * 获取集群成员列表
     * @return
     */
    public CompletableFuture<MemberListResponse> getMemberList();


    /***
     * 续约一个key的lease
     * @param grantId
     * @return
     */
    public CompletableFuture<LeaseKeepAliveResponse> keepAliveOnce(long grantId);


    /***
     * 续约一个key的lease
     * @param grantId
     * @throws Exception
     */
    public void keepAlive(long grantId) throws Exception;


    /***
     * 取消一个key的lease
     * @param grantId
     * @return
     * @throws Exception
     */
    public CompletableFuture<LeaseRevokeResponse> revokeAsync(long grantId) throws Exception;


    /***
     * 取消一个续约信息
     * @param grantId
     * @return
     * @throws Exception
     */
    public LeaseRevokeResponse revokeSync(long grantId) throws Exception;


    /****
     * 监听key的变化
     * @param key
     * @param listener
     * @return
     */
    public Watch.Watcher watchKey(String key, Watch.Listener listener);


    /***
     * 监听目录
     * @param key
     * @param listener
     * @return
     */
    public Watch.Watcher watchPrefKey(String key, Watch.Listener listener);


    /***
     * 获取etcd的事务操作
     * @return
     * @throws Exception
     */
    public Txn getTxn() throws Exception;
}
